﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;

namespace SHomeWorkshop.LunarMind.Adorners
{
    /// <summary>
    /// 创建时间：2013年8月10日
    /// 创建者：  杨震宇
    /// 
    /// 主要用途：为主界面MainScrollViewer添加的装饰器，用于在扩展显示时表示可演示的区域。
    /// </summary>
    public class SlideAdorner : Adorner
    {

        /// <summary>
        /// 用在主界面的靶标（表示插入点）。
        /// </summary>
        /// <param name="adornedElement">应传入备注文本块。</param>
        /// <param name="win">应传入主窗口。</param>
        public SlideAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            //终于解决了选定装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.ToolTip = "分屏演示区域";
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            System.Windows.Forms.Screen[] screens;
            screens = System.Windows.Forms.Screen.AllScreens; //get all the screen width and heights 
            if (screens.Length < 2) return;

            Rect rect = new Rect(AdornedElement.RenderSize);

            //滚动条宽度要去除。
            ScrollViewer scrollViewer = this.AdornedElement as ScrollViewer;
            if (scrollViewer != null)
            {
                if (scrollViewer.ComputedHorizontalScrollBarVisibility == System.Windows.Visibility.Visible)
                {
                    rect.Height -= SystemParameters.HorizontalScrollBarHeight;
                }

                if (scrollViewer.ComputedVerticalScrollBarVisibility == System.Windows.Visibility.Visible)
                {
                    rect.Width -= SystemParameters.VerticalScrollBarWidth;
                }
            }

            if (rect.Width <= screens[1].WorkingArea.Width + 4) return;
            if (rect.Height <= screens[1].WorkingArea.Height + 4) return;

            Brush backbrush = (Brush)FindResource("SlideAdornerBackgroundImageBrush");
            Pen penMain = new Pen(backbrush, 4);

            Pen penOther = new Pen(Brushes.Green, 0);

            Rect boxMainRect = new Rect(rect.Left + (rect.Width - screens[1].WorkingArea.Width) / 2 - 2,
                    rect.Top + (rect.Height - screens[1].WorkingArea.Height) / 2 - 2,
                    screens[1].WorkingArea.Width + 4, screens[1].WorkingArea.Height + 4);

            //左侧
            drawingContext.DrawRectangle(backbrush, penOther,
                new Rect(boxMainRect.Left, boxMainRect.Top + boxMainRect.Height / 2 - 40, 8, 80));

            //上侧
            drawingContext.DrawRectangle(backbrush, penOther,
                new Rect(boxMainRect.Left + boxMainRect.Width / 2 - 40, boxMainRect.Top, 80, 8));

            //右侧
            drawingContext.DrawRectangle(backbrush, penOther,
                new Rect(boxMainRect.Right - 8, boxMainRect.Top + boxMainRect.Height / 2 - 40, 8, 80));

            //下侧
            drawingContext.DrawRectangle(backbrush, penOther,
                new Rect(boxMainRect.Left + boxMainRect.Width / 2 - 40, boxMainRect.Bottom - 8, 80, 8));

            drawingContext.DrawRectangle(null, penMain, boxMainRect);
        }
    }
}